﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MINERP.FactoryGrid.COM;
using System.Configuration;

namespace MINERP.FactoryGrid
{
    public partial class FrmShowGridDetail : Form
    {
        private static string strCon = ConfigurationManager.ConnectionStrings["sqlCon"].ConnectionString;
        private const string TBGETGRIDDETAIL = "TBGetFactoryGridDetail";
        public FrmShowGridDetail()
        {
            InitializeComponent();
        }
        public int GridID { get; set; }

        private void FrmShowGridDetail_Load(object sender, EventArgs e)
        {
            ShowGridDetail();
        }

        private void ShowGridDetail()
        {
            if (GridID > 0)
            {
                using (SqlConnection con = new SqlConnection(strCon))
                {
                    var CMD = con.CreateCommand();
                    CMD.CommandText = "GetFactoryGridDetail";
                    CMD.CommandType = CommandType.StoredProcedure;
                    CMD.Parameters.Add(new SqlParameter ("GridID",GridID));

                    SqlDataAdapter da = new SqlDataAdapter(CMD);

                    DataSet ds = new DataSet();
                    if (ds.Tables[TBGETGRIDDETAIL]!=null)
                    {
                        ds.Tables.Remove(TBGETGRIDDETAIL);
                    }
                    da.Fill(ds, TBGETGRIDDETAIL);
                    gridControl1.DataSource = ds.Tables[TBGETGRIDDETAIL];
                    gridControl1.RefreshDataSource();                    
                }
            }
        }

        private void btnRefush_Click(object sender, EventArgs e)
        {
            ShowGridDetail();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //向该GRID分配访子产品
            AssignGrid.AssignOrderGrid(txtOrderCode.Text.Trim(), GridID);

            ShowGridDetail();

            txtOrderCode.Focus();
            txtOrderCode.SelectAll();
        }

        private void FrmShowGridDetail_KeyDown(object sender, KeyEventArgs e)
        {
            if (!txtOrderCode.Focused)
            {
                txtOrderCode.Focus();
            }           
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (GridID<1)
            {
                return;
            }
            //清空该格子
            using (SqlConnection con = new SqlConnection(strCon))
            {
                if (con.State== ConnectionState .Closed)
                {
                    con.Open();
                }
                var commond = con.CreateCommand();
                commond.CommandText = "update FactorySubOrderInfo set status=0,gridid=0 where gridid=" + GridID;
                commond.CommandType = CommandType.Text;
                commond.ExecuteNonQuery();

                ShowGridDetail();
                MessageBox.Show("清格成功，请重新扫描!");
            }
        }
    }
}
